package com.leetcode.题库;

import com.leetcode.common.model.ListNode;

/**
 * @author 覃国强
 * @date 2022-05-23 16:37
 */
public class 题库_0024_两两交换链表中的节点 {

  static
  class Solution {
    public ListNode swapPairs(ListNode head) {
      ListNode dummy = new ListNode();
      ListNode tail = dummy;

      ListNode front = head;
      ListNode behind = head != null ? head.next : null;

      while (behind != null) {
        front.next = null;

        head = behind.next;
        behind.next = front;
        tail.next = behind;
        tail = front;


        front = head;
        behind = head != null ? head.next : null;
      }

      tail.next = front;
      return dummy.next;
    }
  }

}
